(function($){
    var stat = new Object();
    /*---------------------------------------------------------------------*/
    stat.refresh = function(){
        $("#quesRootList").load(ROOT + "/logined/ques/allQues.do", function(){
            $("#quesRootList a").click(function(){
                $("#quesRootList td").removeClass("highlight");
                var td = $(this).parent();
                td.addClass("highlight");
                stat.show(td.attr("oid"));
            });
        });
    };
    /*---------------------------------------------------------------------*/
    stat.show = function(quesId){
        $("#arena").empty();
        $("#chute").load(ROOT + "/logined/stat/subjects.do", {
            quesId: quesId
        }, function(){
            /* bind action */
            $(".innerMenu a[name=stat]").click(function(){
                exec(quesId);
            });
			 $(".innerMenu a[name=export]").click(function(){
                exportExcel(quesId);
            });
			var links = $(".listActions a");
			$(links[0]).click(function(){
				$(".subjectList :checkbox").each(function(){
					this.checked=true;
				});
			});
			$(links[1]).click(function(){
				$(".subjectList :checkbox").removeAttr("checked");
			});
			$(links[2]).click(function(){
				$(".subjectList :checkbox").each(function(){
					this.checked=!this.checked;
				});
			});
        });
    };
    /*---------------------------------------------------------------------*/
    var _html;
    function exec(quesId){
        var ids = new Array();
        $(".subjectList :checked").each(function(){
            ids.push($(this).val());
        });
        if (_html) {
            loadStat(quesId, ids, _html);
        }
        else {
            $.get(ROOT + "/statitem.jsp", function(s){
                _html = s;
                loadStat(quesId, ids, _html);
            });
        }
    }
    
    function exportExcel(quesId){
        var ids = "";
        $(".subjectList :checked").each(function(){
            ids += "," + $(this).val();
        });
		if(ids.length > 0){
			ids = ids.substr(1);
			$("input[name=ids]").val(ids);
			$("input[name=quesId]").val(quesId);
			$("#exportForm input[name=statType]").val(getStatType());
			$("#exportForm").submit();
		}
    }
	
	function getStatType(){
		if($(":checkbox[name=statType]")[0].checked)
			return 0;
		return 1;
	}
    
    function loadStat(quesId, ids, html){
        var arena = $("#arena");
        arena.empty();
        for (var i in ids) {
            var jq = $(html);
            arena.append(jq);
            jq.load(ROOT + "/logined/stat/show.do", {
                id: ids[i]
				,quesId: quesId
                ,statType: getStatType()
            });
        }
    }
    /*---------------------------------------------------------------------*/
    // Install to jQuery
    $.stat = stat;
})(jQuery);
/*
 * ============================================
 * = main method
 * ============================================
 */
(function($){
    /* main method */
    $(document).ready(function(){
        $.stat.refresh();
    });
})(jQuery);
